import axios from 'axios'
//配置axios
/* 
baseurl 
请求头
超时时间
请求和相应的拦截
*/

//配置baseURL  根据到底是开发环境还是真实环境 进行切换
//axios.defaults.baseURL = process.env.VUE_APP_BASE_URL;
//配置的token  后台设置的token的名字是Authorization  这个是固定koken
//axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
//配置请求头
//axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

const instance=axios.create({
    baseURL:'/api',
    //timeout :''
    //headers
});

instance.interceptors.request.use(function (config) {
    // Do something before request is sent
    let token=sessionStorage.getItem('token');
    token && (config.headers.Authorization=token);
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
instance.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    console.log(response,'response');
    switch(response.data.status){
      case 401:
        window.location.href='http://localhost:8080/login'
        break;
    }
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  });



export default instance;

